<?php

const  TEMPLATE1 = 'QdIQs6JkCuSyNzcDQwGN3AmYR7f-iAVZCgpYFM3m5Jc'; // 工单发布通知

/**
 * 通过code请求接口获取openid
 * @param $code 用户登录code
 * @return mixed|string
 */

function getOpenid($code){
    $url = 'https://api.weixin.qq.com/sns/jscode2session';
    $appid = config('wx.AppID');
    $secret = config('wx.AppSecret');
    $url .= "?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
    $result = \fast\Http::get($url);
    return json_decode($result,true);
}

/**
 * 获取accessToken
 * @return mixed
 */
function getAccessToken(){
    $url = "https://api.weixin.qq.com/cgi-bin/token";
    $appid = config('wx.AppID');
    $secret = config('wx.AppSecret');
    $url .= "?grant_type=client_credential&appid={$appid}&secret={$secret}";
    $result = \fast\Http::get($url);
    return json_decode($result,true);
}



/**
 * @param $code
 * @return mixed
 * 获取微信公众号用户的openid
 */
function getOpenid2($code){
    $url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
    $appid = config('wx.GzhAppID');
    $secret = config('wx.GzhAppSecret');
    $url .= "?appid={$appid}&secret={$secret}&code={$code}&grant_type=authorization_code";
    $result = \fast\Http::get($url);
    return json_decode($result,true);
}

/**
 * @return mixed
 * 获取微信公众号的accessToken
 */
function getAccessToken2(){
    $wechat_access_token = cache('wechat_access_token');
    if($wechat_access_token){
        $result2["access_token"] = $wechat_access_token;
    }else{
        $url = "https://api.weixin.qq.com/cgi-bin/token";
        $appid = config('wx.GzhAppID');
        $secret = config('wx.GzhAppSecret');
        $url .= "?grant_type=client_credential&appid={$appid}&secret={$secret}";
        $result = \fast\Http::get($url);
        $result2 = json_decode($result,true);
        cache('wechat_access_token', $result2['access_token'], 6500);
    }
    return $result2;
}

/**
 * @return mixed
 * 获取微信公众号用户信息
 */
function getWechatUserInfo($wechat_openid){
    $tmp = getAccessToken2();
    $access_token = $tmp["access_token"];
    $url = "https://api.weixin.qq.com/cgi-bin/user/info";
    $url .= "?access_token={$access_token}&openid={$wechat_openid}&lang=zh_CN";
    $result = \fast\Http::get($url);
    return json_decode($result,true);
}


/**
 * @param $wechat_openid  //发送给谁
 * @param array $data  发送内容
 * @param int $template_id 模板id
 * @param string $skip_url 跳转地址
 * @return mixed
 * 微信公众号发送模板消息
 */
function sendMessage($wechat_openid, $data = [], $template_id = 0, $skip_url = ''){
    $tmp = getAccessToken2();
//    $config = Service::getConfig('wechat');
    $token = $tmp["access_token"];
    $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$token";
    $data2 =[];
    if($data){
        foreach ($data as $k=>$v){
            $data2[$k]=[
                "value"=> $v,
            ];
        }
    }
    $data=[
        "touser"=> $wechat_openid,
        "template_id"=>$template_id,
        "miniprogram"=>[
            "appid"=> config('wx.AppID'), //需要跳转小程序的appid
            "path"=>"pages/index/index"
        ],
        "url"=>$skip_url,
        "topcolor">"#FF0000",
        'data'=>$data2
    ];

    $info = \fast\Http::post($url,json_encode($data,true),'json');
    \think\Log::log(['tmbb'=>$info]);
    // 一定要注意转json，否则汇报47001错误
    $tmpinfo = json_decode($info,true);
    return $tmpinfo;
}

